
package model;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Inet4Address;
import java.net.Socket;
import java.net.UnknownHostException;

import utils.Command;
import utils.MyConnection;

public class ClientModel {
    
    private String ip;
    private int port;
    private Socket socket;
    private ObjectOutputStream out;
    private ObjectInputStream in;
    
    public ClientModel(String ip, int port) {
        this.ip = ip;
        this.port = port;
    }
    
    public boolean connect() {
        try {
            socket = new Socket(ip, port);
            out = new ObjectOutputStream(socket.getOutputStream());
            in = new ObjectInputStream(socket.getInputStream());
            return true;
        } catch (IOException e) {
            return false;
        }
    }
    
    public String[] receiveAnswerTo(String command, String message) {
        String[] receivedSeparated = new String[2];
        String receivedTotal;
        try {
            out.writeObject(String.valueOf(command + message));
            out.flush();
            out.reset();
            receivedTotal = String.valueOf(in.readObject());
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
            receivedTotal = Command.SOCKET_FAIL;
        }
        receivedSeparated[0] = receivedTotal.substring(0, 3);
        if (receivedTotal.length() > 3) {
            receivedSeparated[1] = receivedTotal.substring(3);
        } else {
            receivedSeparated[1] = "";
        }
        return receivedSeparated;
    }
    
    public void receiveFile(String nodeName, String fileName)
            throws IOException {
        
        Socket socketP2P = new Socket(nodeName, port + 1);
        
        InputStream inAux = socketP2P.getInputStream();
        ObjectOutputStream objOut = new ObjectOutputStream(
                socketP2P.getOutputStream());
        OutputStream outAux = new FileOutputStream(fileName);
        
        objOut.writeObject(String.valueOf(Command.READ_FILE + fileName));
        objOut.flush();
        
        MyConnection.copy(inAux, outAux);
        inAux.close();
        outAux.close();
        objOut.close();
        socketP2P.close();
    }
    
    public void sendFile(String nodeNames, String fileName) throws IOException {
        String[] nodeNamesList = nodeNames.split(",");
        
        for (int i = 0; i < nodeNamesList.length; i++) {
            Socket socketP2P = new Socket(nodeNamesList[i], port + 1);
            
            ObjectOutputStream objOut = new ObjectOutputStream(
                    socketP2P.getOutputStream());
            File file = new File(fileName);
            InputStream inAux = new FileInputStream(file);
            OutputStream outAux = socketP2P.getOutputStream();
            
            objOut.writeObject(String.valueOf(Command.UPDATE_FILE + fileName));
            objOut.flush();
            
            MyConnection.copy(inAux, outAux);
            inAux.close();
            outAux.close();
            objOut.close();
            socketP2P.close();
        }
        
    }
    
    public boolean createFile(String fileName) {
        File file = new File(fileName);
        try {
            file.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
        return true;
    }
    
    public void deleteFile(String fileName) {
        File file = new File(fileName);
        file.delete();
    }
    
    public void printTime(String timeCreate, long timeTotal) throws IOException {
        
        int timeInSeconds = (int) (timeTotal / 1000);
        
        File file = new File(timeCreate);
        
        if (!file.exists()) {
            file.createNewFile();
        }
        BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
        
        writer.write(String.valueOf(timeTotal));
        writer.newLine();
        
        writer.close();
    }
    
    public void closeConnection() {
        try {
            socket.close();
            out.close();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public String getMyIp() {
        try {
            return Inet4Address.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }
    
    public String getIp() {
        return ip;
    }
    
    public void setIp(String ip) {
        this.ip = ip;
    }
    
    public int getPort() {
        return port;
    }
    
    public void setPort(int port) {
        this.port = port;
    }
    
}
